#include "onu_comm.h"


static char Svr_host[64] = "10.0.0.25", Storage_path[32] = {0};
static uint16_t Tcp_port = 18087, Udp_port = 17087;
static OPT_T opts[] = {
    { "-a", Svr_host, OPT_STRING, "server address." },
    { "-n", &Tcp_port, OPT_SHORT, "tcp port." },
    { "-u", &Udp_port, OPT_SHORT, "udp port." },
    { "-s", &Storage_path, OPT_STRING, "storage path." },
};

#define NET_CHECK_MAX_COUNT 10

static void start_Monitor(){
    char cmd[512] = {0};

    system_killall(ONU_MONITOR_EXE);
    
    sprintf(cmd, "./%s -a %s -n %d -u %d &", ONU_MONITOR_EXE, Svr_host, Tcp_port, Udp_port);
    log_trace("%s", cmd);
    system(cmd);
}

int onu_Router(int argc, char** argv)
{

    DevInfo_t* devinfo = &devInfo;
    Platform_t* platform;
    uint32_t wait_sec = 1;
    int net_check_count = 0, ppp_account_get_ret = 0;
    char pppoe_usr[64] ={0}, pppoe_pwd[64] = {0};
    char svr_ipstr[32] = {0};

    platform = &onuProbe.platform;

    if (0 > opt_parse(opts, sizeof(opts), argc, argv)) {
        return 1;
    }

    if(strlen(Storage_path) > 0 ){
        chdir(Storage_path);
    }

    server_addr_to_ipstr(Svr_host);

    start_Monitor();

    check_gateway_interface(devinfo->ifname);
    load_devinfo(devinfo);

    init_comm_req_param(&commReqParam, devinfo);

    platform_init(platform, Svr_host, Tcp_port, Udp_port, "http");

retry:
    wait_sec = _cal_wait_sec(wait_sec);
    if (!platform_auth(platform)) {
        log_warn("Platform Auth failed.");
        sleep(wait_sec);
        goto retry;
    }

    /*for test*/
   // strcpy(commReqParam.bdaccount, "xxxx");
   // strcpy(commReqParam.bdpassword, "xxxx");

    if(strlen(commReqParam.bdaccount) > 0 && strlen(commReqParam.bdpassword) > 0){
        //桥模式下才需要修改路由模式连接账号
        if(devinfo_is_bridge()){
            log_trace("dev is bridge mode.");
            //当onuProbe 已经运行起来，说明肯定有路由模式的连接，否则onuLoader不可能把onuProbe下载下来
            ppp_account_get_ret = devinfo_get_internet_r_usr_pwd(pppoe_usr, pppoe_pwd);
            log_trace("get pppoe usr:%s, pwd:%s, ppp_account_get_ret:%d", pppoe_usr, pppoe_pwd, ppp_account_get_ret);
            //如果是预置账号，则删除掉，换成用户账号
            if(ppp_account_get_ret && strcmp(pppoe_usr, PRE_PPPOE_USER) == 0){
                log_trace("dev del pre account:"PRE_PPPOE_USER" "PRE_PPPOE_PWD".");
                devinfo_del_pppoe_dial(PRE_PPPOE_USER, PRE_PPPOE_PWD);
                if(devinfo_has_internet_r() == 0){
                    log_trace("dev add usr account:%s %s", commReqParam.bdaccount, commReqParam.bdpassword);
                    devinfo_add_pppoe_dial(commReqParam.bdaccount, commReqParam.bdpassword);
                    strcpy(commReqParam.dial_bdaccount, commReqParam.bdaccount);
                    sleep(10);
                }
            }
            

net_check:
            log_trace("check gateway... %d", net_check_count);
            if(check_gateway_interface(devinfo->ifname) == 0){
                if(net_check_count == NET_CHECK_MAX_COUNT){

                    memset(pppoe_usr, 0, sizeof(pppoe_usr));
                    memset(pppoe_pwd, 0, sizeof(pppoe_pwd));

                    ppp_account_get_ret = devinfo_get_internet_r_usr_pwd(pppoe_usr, pppoe_pwd);
                    log_trace("get pppoe usr:%s, pwd:%s, ppp_account_get_ret:%d", pppoe_usr, pppoe_pwd, ppp_account_get_ret);
                    if(ppp_account_get_ret){
                        log_trace("pppoe del user:%s,pwd:%s", pppoe_usr, pppoe_pwd);
                        devinfo_del_pppoe_dial(pppoe_usr, pppoe_pwd);
                    }
                    
                    if(devinfo_has_internet_r() == 0){
                        log_trace("pppoe add user:%s,pwd:%s", PRE_PPPOE_USER, PRE_PPPOE_PWD);
                        devinfo_add_pppoe_dial(PRE_PPPOE_USER, PRE_PPPOE_PWD);
                        strcpy(commReqParam.dial_bdaccount, PRE_PPPOE_USER);
                    }else{
                        log_trace("has internet router mode connection.");
                    }
                    
                }
                net_check_count++;
                sleep(30);
                goto net_check;
            }
        }
    }

    init_heartinfo(&platform->heartinfo, commReqParam.probeId, 2,
        platform->addr.host, platform->addr.udp_port);

    if (!platform_dial_req(platform)) {
        log_warn("Platform request dial task failed.");
        sleep(wait_sec);
        goto retry;
    }

    platform_task_run(platform);

    return 1;
}
